﻿Imports Abyx.SVCMS.Library
Imports Abyx.WindowsExtendedControls
Imports Abyx.SVCMS.Library.DB

Public NotInheritable Class SVCM_SplashScreen

    Private Sub SVCM_SplashScreen_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Debugger.IsAttached AndAlso My.Settings.SkipLoginWhenDebugging Then
            DoDatabaseConnectivityChecks()
            SetupCaptions()
            If AppSettings.Dont_Load_Updates_At_Startup = False Then
                ApplicationLoader_BackgroundWorker.RunWorkerAsync()
            End If
            Dim user As UserType = SecManager.Login("admin", "admin")
            If user IsNot Nothing Then
                AppSettings.LoggedUser = user
                AppSettings.MainForm = SVCM_Form
                SVCM_Form.Show()
                Me.Close()
            Else
                AMsg.ShowDialog("Authentication failed....", MsgBoxStyle.Critical)
                Application.Restart()
                Me.Close()
            End If
        Else
            DoDatabaseConnectivityChecks()
            SetupCaptions()
            If AppSettings.Dont_Load_Updates_At_Startup = False Then
                ApplicationLoader_BackgroundWorker.RunWorkerAsync()
            Else
                'Load the main form
                SEC_Login_Form.Show()

                'Close the splash screen
                Me.Close()
            End If
        End If

    End Sub

    Private Sub DoDatabaseConnectivityChecks()

        If SysManager.DataBaseConnectivityTest() = False Then
            AMsg.ShowDialog(AppConstants.Error_Message_Db_Connectivity_Failed, MsgBoxStyle.Critical)
            If AMsg.ShowDialog("Do you want to reconfigure database " & _
                               "connectivity settings", MsgBoxStyle.Question) = MsgBoxResult.Yes Then
                Dim DLG As SQL_Connection_Dialog = New SQL_Connection_Dialog
                DLG.ConnectionString = SysManager.GetConnectionString
                If DLG.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    SysManager.SetConnectoinString(DLG.ConnectionString)
                    Application.Restart()
                Else
                    Application.Exit()
                End If
            Else
                Application.Exit()
            End If
        End If

    End Sub

    Delegate Sub SetupCaptionsDelegate()
    Private Sub SetupCaptions()
        'SetupCaptionsDelegate
        If ApplicationTitle_Label.InvokeRequired Then
            Dim d As New SetupCaptionsDelegate(AddressOf SetupCaptions)
            ApplicationTitle_Label.Invoke(d)
        Else
            If My.Application.Info.Title <> "" Then
                ApplicationTitle_Label.Text = My.Application.Info.Title
            Else
                ApplicationTitle_Label.Text = System.IO.Path.GetFileNameWithoutExtension(My.Application.Info.AssemblyName)
            End If
            Version_Label.Text = System.String.Format(Version_Label.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor)
            Copyright_Label.Text = My.Application.Info.Copyright
        End If
    End Sub

    Private Sub ApplicationLoader_BackgroundWorker_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles ApplicationLoader_BackgroundWorker.DoWork

        Dim preLoader As New AppPreLoader(Status_Label)
        preLoader.DoPreLoadingTasks()

    End Sub

    Private Sub ApplicationLoader_BackgroundWorker_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles ApplicationLoader_BackgroundWorker.RunWorkerCompleted

        If (Debugger.IsAttached = False) OrElse (My.Settings.SkipLoginWhenDebugging = False) Then
            'Load the main form
            SEC_Login_Form.Show()

            'Close the splash screen
            Me.Close()
        End If


    End Sub

End Class
